package com.baidu.service.impl;
import com.baidu.entity.WycNote;
import com.baidu.mapper.WycNoteMapper;
import com.baidu.service.NoteService;
import com.baidu.util.AuthCodeUtil;
import com.baidu.utils.ResultResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
/**
 * @Description: 短信实现类
 * @Author: 王雨超
 * @Date: 2021/12/13 16:32
 */
@SuppressWarnings({"all"})
@Service
@Slf4j
public class NoteServiceImpl implements NoteService {
    //注册模板id
    private static final String register="a09602b817fd47e59e7c6e603d3f088d";
    @Autowired
    private WycNoteMapper wycNoteMapper;
    /**
     *@Description: 短信方法
     *@Author: 王雨超
     *@Date: 2021/12/14
     *@Param:  map
     *@return: map
     */
    @Override
    public ResultResponse sendMess(HashMap<String, Object> map) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        HashMap<String, Object> map1 = new HashMap<>();
        String code=(String)map.get("code"); //判断是发送什么样的短信
        String message=(String)map.get("message"); //发送的内容
        String phone=(String)map.get("phone");//手机号
        String uid=(String)map.get("uid");//用户id
        //如果是1 证明是注册
        if(code.equals("1")){
            //获取验证码
            String code1 = AuthCodeUtil.getCode(phone, 6);
            //发送短信
            boolean b = AuthCodeUtil.sendMsgToPhone(phone, code1,register);
            if(b){
                //记录在数据库
                WycNote wycNote = new WycNote();
                wycNote.setUid(Integer.parseInt(uid));
                wycNote.setPhone(phone);
                wycNote.setContent("验证码："+code1);
                wycNote.setTemplate(register);
                wycNote.setPuttime(new Date());
                wycNoteMapper.insert(wycNote);
                //打印日志
                log.info("{}发送了一条短信：",wycNote);
                //返回验证码
                return ResultResponse.SUCCESS(code1);
            }
            return ResultResponse.FAILED();
            //如果不是 证明不需要验证码
        }else{
            //发送短信
            boolean b = AuthCodeUtil.sendMsgsToPhone(phone,message, register);
            if(b){
                //记录在数据库
                WycNote wycNote = new WycNote();
                wycNote.setUid(Integer.parseInt(uid));
                wycNote.setPhone(phone);
                wycNote.setContent(message);
                wycNote.setTemplate(register);
                wycNote.setPuttime(new Date());
                wycNoteMapper.insert(wycNote);
                log.info("{}发送了一条短信：",wycNote);
                return ResultResponse.SUCCESS();
            }
            return ResultResponse.FAILED();
        }
    }
}
